package com.xxchat.controller;

import com.xxchat.annotation.GlobalInterceptor;
import com.xxchat.entity.dto.TokenUserInfoDTO;
import com.xxchat.entity.dto.UserContactSearchResultDTO;
import com.xxchat.entity.enums.ResponseCodeEnum;
import com.xxchat.entity.enums.UserContactStatusEnum;
import com.xxchat.entity.enums.UserContactTypeEnum;
import com.xxchat.entity.po.UserContact;
import com.xxchat.entity.po.UserContactApply;
import com.xxchat.entity.po.UserInfo;
import com.xxchat.entity.query.UserContactQuery;
import com.xxchat.entity.vo.PaginationResultVO;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.entity.vo.UserInfoVO;
import com.xxchat.exception.BusinessException;
import com.xxchat.service.UserContactApplyService;
import com.xxchat.service.UserContactService;
import com.xxchat.service.UserInfoService;
import com.xxchat.utils.CopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Waicc
 */
@RestController("userContactController")
@RequestMapping("/contact")
@Validated
public class UserContactController extends ABaseController {
    @Resource
    private UserContactService userContactService;

    @Resource
    private UserContactApplyService contactApplyService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserContactApplyService userContactApplyService;

    @RequestMapping("/search")
    @GlobalInterceptor
    public ResponseVO search(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        UserContactSearchResultDTO resultDTO = this.userContactService.searchContact(userInfoDTO.getUserId(), contactId);
        return getSuccessResponseVO(resultDTO);
    }

    @RequestMapping("/apply")
    @GlobalInterceptor
    public ResponseVO apply(HttpServletRequest request, @NotEmpty String contactId, String applyInfo) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        Integer joinType = this.contactApplyService.apply(userInfoDTO, contactId, applyInfo);
        return getSuccessResponseVO(joinType);
    }

    @RequestMapping("/loadApplyList")
    @GlobalInterceptor
    public ResponseVO loadApplyList(HttpServletRequest request, Integer pageNo) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        PaginationResultVO<UserContactApply> applyList = this.userContactApplyService.loadApplyList(userInfoDTO, pageNo);
        return getSuccessResponseVO(applyList);
    }

    @RequestMapping("/handleApply")
    @GlobalInterceptor
    public ResponseVO handleApply(HttpServletRequest request, @NotNull Integer applyId, @NotNull Integer status) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        this.userContactApplyService.handlerApply(userInfoDTO.getUserId(), applyId, status);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadContactList")
    @GlobalInterceptor
    public ResponseVO loadContactList(HttpServletRequest request, @NotEmpty String contactTypeName) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        UserContactTypeEnum contactTypeEnum = UserContactTypeEnum.getByName(contactTypeName);
        if (null == contactTypeEnum) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        List<UserContact> contactList = this.userContactService.loadContactList(userInfoDTO, contactTypeEnum);
        return getSuccessResponseVO(contactList);
    }

    @RequestMapping("/getContactInfo")
    @GlobalInterceptor
    public ResponseVO getContactInfo(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);

        UserInfo contactInfo = this.userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO contactInfoVO = CopyUtils.copy(contactInfo, UserInfoVO.class);
        contactInfoVO.setContactStatus(UserContactStatusEnum.NOT_FRIEND.getStatus());

        UserContact userContact = this.userContactService.getUserContactByContactIdAndUserId(userInfoDTO.getUserId(), contactId);
        if (null != userContact) {
            // TODO 判断关系状态，给合适的值
            contactInfoVO.setContactStatus(UserContactStatusEnum.FRIEND.getStatus());
        }

        return getSuccessResponseVO(contactInfoVO);
    }

    @RequestMapping("/delContact")
    @GlobalInterceptor
    public ResponseVO delContact(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        this.userContactService.removeContact(userInfoDTO.getUserId(), contactId, UserContactStatusEnum.DEL);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/addContact2BlackList")
    @GlobalInterceptor
    public ResponseVO addContact2BlackList(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        this.userContactService.removeContact(userInfoDTO.getUserId(), contactId, UserContactStatusEnum.BLACKLIST);
        return getSuccessResponseVO(null);
    }
}
